﻿using System;
using System.Drawing.Imaging;
using System.Linq;
using System.Web.Mvc;
using STX.Service;
using STX.Domain;

using STX.Admin.Models;

namespace STX.Admin.Controllers
{
    public abstract class BasePages1Controller<T, TI, TA, TE, TQ> : AuthController
        where T : BaseEntity
        where TI : BaseIndexModel, new()
        where TA : BaseAddModel, new()
        where TE : BaseEditModel, new()
        where TQ : BaseQueryModel, new()
    {
        #region 字段

        private Func<BaseService<T>> _getTheServiceFunc;

        private Func<TI> _getIndexModelFunc;
        private Func<TA> _getAddModelFunc;
        private Func<int, TE> _getEditModelFunc;
        #endregion

        #region 属性

        private BaseService<T> Service
        {
            get
            {
                return _getTheServiceFunc();
            }
        }

        #endregion

        #region 构造函数

        protected BasePages1Controller()
        {
            // ReSharper disable once DoNotCallOverridableMethodsInConstructor
            OnInit(out _getTheServiceFunc, out _getIndexModelFunc, out _getAddModelFunc, out _getEditModelFunc);
            
        }
        #endregion

        #region Method

        #region 抽象方法
        protected abstract void OnInit(out Func<BaseService<T>> thisService ,out Func<TI> getIndexModelFunc,
            out Func<TA> getAddModelFunc,
            out Func<int, TE> getEditModelFunc);

        protected abstract long AddHandler(TA addModel);

        protected abstract bool EditHandler(TE editModel);

        protected abstract IQueryable<T> QueryHandler(TQ queryModel, IQueryable<T> queryable);

        protected virtual IQueryable<T> OrderByHandler(IQueryable<T> queryable)
        {
            return queryable.OrderByDescending(o => o.Id);
        }

        #endregion

        #region Methods 首页
        public virtual ActionResult Index()
        {
            return View(_getIndexModelFunc());
        }
        #endregion

        #region Methods 查询

        public virtual ActionResult Query()
        {
            return Query(new TQ
            {
                Page = 1,
                PageSize = 20
            });
        }

        [HttpPost]
        public virtual ActionResult Query(TQ query)
        {
            var result = Service.FindAll();
            result = QueryHandler(query, result);
            var total = result.Count();
            result = OrderByHandler(result);
            if (query.Page > 1 && query.PageSize > 0)
            {
                result = result.Skip((query.Page - 1) * query.PageSize);
            }
            result = result.Take(query.PageSize);
            var data = new Web.Framework.Query.PagedList<T>()
            {
                Page = query.Page,
                PageSize = query.PageSize,
                Data = result.ToList(),
                Total = total
            };
            return View(data);
        }

        #endregion

        #region Methods 添加

        public virtual ActionResult Add()
        {
            return View(_getAddModelFunc());
        }

        [HttpPost]
        [ValidateInput(false)]
        public virtual JsonResult SaveAdd(TA model)
        {
            AddHandler(model);
            return new JsonResult { Data = new { success = true } };
        }

        #endregion

        #region Methods 删除

        public virtual JsonResult Delete(int[] ids)
        {
            Service.RemoveIds(ids);
            return new JsonResult { Data = new { success = true } };
        }

        #endregion

        #region Methods编辑

        public virtual ActionResult Edit(int id)
        {
            return View(_getEditModelFunc(id));
        }

        [HttpPost]
        [ValidateInput(false)]
        public virtual JsonResult SaveEdit(TE model)
        {
            EditHandler(model);
            return new JsonResult { Data = new { success = true } };
        }

        #endregion

        #endregion

    }
}
